package com.hpsk.shopstart.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import com.hpsk.common.CommonException;
import com.hpsk.common.RespResult;
import com.hpsk.common.RespUtils;
import com.hpsk.shopstart.handler.GlobalExceptionUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.stream.Collectors;

/**
 * 全局异常处理
 * @author huangwang2832511019
 */
@RestControllerAdvice
@Slf4j
public class EaxceptionControllerAdvice {

    @ExceptionHandler(CommonException.class)
    @ResponseBody
    public RespResult<String> handleException(HttpServletRequest request, CommonException e){
        log.error(">>> 请求路径：{}\t >>> 异常原因：{}",request.getRequestURI(),e.getMessage());
        return RespUtils.err(e.getMessage());
    }
    @ResponseBody
    @ExceptionHandler
    public RespResult<String> handle3Exception(Exception e) {
        return GlobalExceptionUtil.getCommonResult(e);
    }
    @ExceptionHandler(value = {RuntimeException.class,BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    public RespResult<String> handleException(Exception e) {
        String result = null;
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            log.info("异常MethodArgumentNotValidException！:{}", ex.getBindingResult().toString());
            String errorMessages = ex.getBindingResult().getAllErrors().stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.joining(";"));
            result = errorMessages;
        } else if (e instanceof ConstraintViolationException) {
            //校验异常
            ConstraintViolationException ex = (ConstraintViolationException) e;
            log.info("异常ConstraintViolationException！:{}", ex.getConstraintViolations().toString());
            String errorMessages = ex.getConstraintViolations().stream()
                    .map(ConstraintViolation::getMessage)
                    .collect(Collectors.joining(";"));
            result = errorMessages;
        } else if (e instanceof BindException) {
            BindException ex = (BindException) e;
            log.info("getAllErrors！:{}", ex.getAllErrors().toString());
            String errorMessages = ex.getAllErrors().stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.joining(";"));
            result = errorMessages;
        }else if(e instanceof RuntimeException){

            result = e.getMessage();
        }
        log.error("异常！:{}", result);

        return RespUtils.err(result);
    }

}
